<template>
    <rx-dialog @ok="handleCompleteCheck" @cancel="closeWindow"
               :okText="$ti18('rxForm.commitBtn','提交')"
               :btnAlign="'center'">
        <rx-fit>
            <template #middle>
                <div class="task-bat-div" style="height:100%;width:100%;padding:20px;">
                    <a-form :label-col="labelCol" :wrapper-col="wrapperCol" style="padding-bottom: 10px;">
                        <a-form-item :label="i18n('taskApply.selectedTask','已选择任务')">
                            <rx-grid ref="table"
                                     style="min-height: 500px;"
                                     :data="tasksInfo"
                                     :selectConfig="selectConfig"
                                     :hasPage="false"
                                     :columns="columns"
                                     field="taskId"
                            >
                                <template #nodeExecutors="{row,rowIndex}">
                                    <bpm-task-executors
                                        ref="taskExecutors"
                                        :nodeExecutors="row.nodeExecutors"
                                        :allow-select-executor="row.allowSelectExecutor"
                                        :allow-select-group="row.allowSelectGroup"
                                        :task-config="row.taskConfig"
                                        :processConfig="row.processConfig">
                                    </bpm-task-executors>
                                </template>
                                <template #isBatch="{row,rowIndex}">
                                    <a-tag :color="row.isBatch==='YES' ? 'green' : 'red'">
                                        <span v-if="row.isBatch==='YES'">{{ i18n('taskApply.permit', '允许') }}</span>
                                        <span
                                            v-else-if="row.isBatch==='NO'">{{ i18n('taskApply.notAllow', '不允许') }}</span>
                                    </a-tag>
                                </template>
                                <template #status="{row,rowIndex}">
                                    <a-tag :color="statusMap[row.status].color">
                                        <span :title="row.message">{{ statusMap[row.status].text }}</span>
                                    </a-tag>
                                </template>
                                <template #action="{ row, rowIndex }">
                                    <RxGridAction :size="2">
                                        <div @click.stop="del(rowIndex)">{{ $ti18('rxList.delBtn', '删除') }}</div>
                                    </RxGridAction>
                                </template>
                            </rx-grid>
                        </a-form-item>
                        <a-form-item :label="i18n('taskApply.applyAction','审批动作')">
                            <a-radio-group v-model:value="form.checkType">
                                <a-radio value="AGREE">{{ i18n('taskApply.agree', '同意') }}</a-radio>
                                <a-radio value="BACK_TO_STARTOR">{{ i18n('taskApply.reject', '驳回到发起人') }}</a-radio>
                            </a-radio-group>
                        </a-form-item>
                        <a-form-item :label="i18n('taskApply.opinion','意见')">
                            <a-textarea :placeholder="i18n('taskApply.opinion','请填写意见','write')"
                                        v-model:value="form.opinion"
                                        :autoSize="{ minRows: 2, maxRows: 6 }"></a-textarea>
                        </a-form-item>
                    </a-form>
                </div>
            </template>
        </rx-fit>
    </rx-dialog>
</template>
<script>
import {RxGrid, RxDialog, Util, RxFit} from '@lowcode/jpaas-base-lib';
import BpmtaskApi from '@/common/api/bpm/bpmTask';
import BpmTaskExecutors from "../components/BpmTaskExecutors.vue";

export default {
    name: 'bpm-task-check',
    components: {
        RxFit,
        RxDialog,
        RxGrid,
        BpmTaskExecutors
    },
    props: {
        layerid: String,
        destroy: Function,
        taskIds: String
    },
    data() {
        return {
            labelCol: {span: 4},
            wrapperCol: {span: 20},
            selectConfig: {
                type: 'checkbox'
            },
            form: {
                taskIds: this.taskIds,
                checkType: "AGREE",
                opinion: ""
            },
            columns: [
                {title: this.i18n('taskApply.name', '标题'), field: 'subject', width: 250},
                {
                    title: this.i18n('taskApply.approver', '审批人'),
                    field: 'nodeExecutors',
                    slots: {default: 'nodeExecutors'},
                    showOverflow: false
                },
                {
                    title: this.i18n('taskApply.allowableBatch', '允许批量'),
                    field: 'isBatch',
                    width: 80,
                    slots: {default: 'isBatch'}
                },
                {title: this.$ti18('rxList.action', '操作'), width: 80, field: 'action', slots: {default: 'action'}},
                {title: this.i18n('form.status', '状态'), width: 100, field: 'status', slots: {default: 'status'}}
            ],
            //已选择任务详情
            tasksInfo: [],
            statusMap: {
                0: {color: 'blue', text: this.i18n('statusMap.notApproved', '未审批')},
                1: {color: 'green', text: this.i18n('statusMap.approvedSuccess', '审批成功')},
                2: {color: 'red', text: this.i18n('statusMap.approvedFailure', '审批失败')},
            },
        }
    },
    created() {
        this.getTaskInfo();
    },
    methods: {
        i18n(name, text, key) {
            return this.$ti18(name, text, "MyBpmTask", key);
        },
        //获取已选择的任务
        getTaskInfo() {
            BpmtaskApi.getTasksInfo(this.taskIds).then(res => {
                if (res && res.data) {
                    var tasks = [];
                    for (let i = 0; i < res.data.length; i++) {
                        if (res.data[i].bpmTask) {
                            tasks.push(res.data[i]);
                        }
                    }
                    this.initTaskInfo(tasks);
                }
            });
        },
        //初始化任务格式
        initTaskInfo(tasks) {
            for (let i = 0; i < tasks.length; i++) {
                var obj = {
                    taskId: tasks[i].bpmTask.pkId,
                    subject: tasks[i].bpmTask.subject,
                    nodeExecutors: tasks[i].nodeExecutors,
                    taskConfig: tasks[i].taskConfig,
                    status: "0",
                    message: this.i18n('statusMap.notApproved', '未审批'),
                    processConfig: tasks[i].processConfig
                }
                //计算是否允许批量审批
                var processConfig = tasks[i].processConfig;
                var taskConfig = tasks[i].taskConfig;
                obj.allowSelectExecutor = taskConfig.switchOptions.includes("allowSelectExecutor");
                obj.allowSelectGroup = taskConfig.switchOptions.includes("allowSelectGroup");
                var rtn = processConfig.switchOptions.includes("notAllowBatch");
                if (!rtn) {
                    rtn = taskConfig.switchOptions.includes("notAllowBatch");
                }
                //YES 允许 NO 不允许
                obj.isBatch = rtn ? "NO" : "YES";
                this.tasksInfo.push(obj);
            }
        },
        closeWindow() {
            Util.closeWindow(this, 'cancel')
        },
        handleCompleteCheck(vm) {
            var self_ = this;
            var taskIds = [];
            this.tasksInfo.find(item => {
                //允许批量审批且未审批过
                if (item.isBatch == "YES" && item.status != "1") {
                    taskIds.push(item.taskId);
                }
            });
            if (taskIds.length == 0) {
                vm.loading = false;
                this.$message.warning(this.i18n('msa.unEnforceableTask', '无可执行的任务!'));
                return;
            }
            self_.form.taskIds = taskIds.join(",");
            this.$confirm({
                title: this.$ti18('rxMsa.tipsInfo', '提示信息'),
                content: this.i18n('msa.confirmApplyTips', '确认审批吗?'),
                okText: this.$ti18('rxForm.ackBtn', '确认'),
                cancelText: this.$ti18('rxForm.cancelBtn', '取消'),
                zIndex: 30000,
                onOk() {
                    BpmtaskApi.batApprove(self_.form).then(res => {
                        if (res && res.data) {
                            for (let i = 0; i < res.data.length; i++) {
                                var data = res.data[i];
                                self_.tasksInfo.find(item => {
                                    if (item.taskId == data.taskId) {
                                        item.status = data.result.success ? "1" : "2";
                                        item.message = data.result.message;
                                    }
                                });
                            }

                            let status = res.code;
                            let message = res.message;
                            switch (status) {
                                case 0:
                                    self_.$message.success(message);
                                    break;
                                case -1:
                                    self_.$message.error(message);
                                    break;
                                case 1:
                                    self_.$message.warn(message);
                                    break;
                            }
                            Util.closeWindow(self_, "ok");
                        }
                        vm.loading = false;
                    })
                },
                onCancel() {
                    vm.loading = false;
                }
            });

        },
        del(index) {
            this.tasksInfo.splice(index, 1);
        }
    }
}
</script>
<style scoped>
.task-bat-div ::v-deep(.ant-row){
    width: 100%;
}
</style>
